﻿using System;
using System.Data;
using System.Web;
using System.Web.SessionState;
using FS.Business;
using FS.Common.DotNetBean;
using FS.Common.DotNetJson;
using FS.Common.DotNetCode;

namespace FS.Web.BLL.Visa_info
{
    /// <summary>
    /// Summary description for VisInfo
    /// </summary>
    public class VisaInfo : IHttpHandler, IRequiresSessionState
    {

        private visa_dao _visa = new visa_dao();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1.0);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            string Action = context.Request["action"];            
            string cityid = context.Request["cityid"];
            string certtype = context.Request["certtype"];
            string certtypename = context.Request["certtypename"];
            string trustid = context.Request["trust_id"];
            string trustname = context.Request["trust_name"];
            string trusttype = context.Request["trusttype"];
            string supplyid = context.Request["supplyid"];
            string supplyname = context.Request["supplyname"];

            string cusname=context.Request["cusname"];
            string cusphone=context.Request["cusphone"];
            string cuscardno=context.Request["cuscardno"];
            string cusdata=context.Request["cusdata"];
            string cus_operation=context.Request["cus_operation"];
            string cprice=context.Request["cprice"];
            string cus_rmk=context.Request["cus_rmk"];
            string infoid = context.Request["infoid"];
            string detailid = context.Request["detailid"];

            string info_operation = context.Request["info_operation"];
            string info_getdate = context.Request["info_getdate"];
            string ls_AddNo = context.Request["AddNo"];

            string text = Action;
            if (text != null)
            {                
                if (text == "GetCertType") //根据城市获取签证类型
                {
                    DataTable dt = _visa.GetSelect_CertTypeInfoByCityID(cityid);
                    string sb = JsonHelper.DataTableToJsonData(dt);

                    context.Response.Write(sb.ToString());
                    context.Response.End();
                }
                else if (text == "GetSupplybycitycert")    //获取报价的供应商
                {
                    DataTable dt = _visa.GetSelect_SupplyInfoByCityIDAndCertType(cityid, certtype);

                    string sb = JsonHelper.DataTableToJsonData(dt);
                    context.Response.Write(sb.ToString());
                    context.Response.End();
                }
                else if (text == "GetPricebycitycert")    //获取国家+签证类型的报价
                {
                    string ls_price = _visa.Ajax_GetPriceFromCityCert(cityid, certtype, trusttype);
                    context.Response.Write(ls_price);
                    context.Response.End();
                }
                else if (text == "GetTrustInfo")    //根据委托方信息
                {
                    string strMenus = "";
                    DataTable dt = _visa.GetSelect_trustInfoBytrustid(trustid);
                    if (dt.Rows.Count > 0)
                    {
                        strMenus = dt.Rows[0]["type_trust"].ToString() + "@" +
                            dt.Rows[0]["type_price"].ToString() + "@" +
                            dt.Rows[0]["trust_contactnumber"].ToString() + "@" +
                            dt.Rows[0]["trust_contactuser"].ToString() + "@" +
                            dt.Rows[0]["keycode"].ToString() + "@" +
                            dt.Rows[0]["trust_user"].ToString();
                    }
                    context.Response.Write(strMenus);
                    context.Response.End();
                }
                else if (text == "InsertInfoDetail")    //插入订单详细信息
                #region
                {
                    string ls_createuserid = RequestSession.GetSessionUser().UserId.ToString();
                    string ls_createusername = RequestSession.GetSessionUser().UserName.ToString();
                    string rtn = "";
                    try {
                        if (cprice == "") { cprice = "0"; }
                        decimal ldc_price = Convert.ToDecimal(cprice);  //应收费用
                        decimal ldc_sprice = 0; //应付费用
                        if (certtype == "s")   //新建签证类型
                        {
                            certtype = this.InsertCertType(cityid, certtype, certtypename, ldc_price, ls_createuserid, ls_createusername);
                            
                        }
                        if (supplyid == "s")   //新建供应商
                        {
                            supplyid = InsertSupply(cityid, certtype, supplyid, supplyname, ldc_sprice, ls_createuserid, ls_createusername);                           
                        }

                        //检查用户是否重复
                        string ls = _visa.Check_InfoDetail(infoid, cusname, cityid, certtype, detailid);
                        if (string.IsNullOrEmpty(ls))
                        {
                            //数据保存
                            bool isok = _visa.Ajax_InsertInfoDetailNew(infoid,cusname,cuscardno,cusphone,cityid,certtype,
                                ldc_price, supplyid, ls_createuserid, ls_createusername, cus_operation, cusdata, cus_rmk, detailid);
                            if (isok)
                            {
                                rtn = String.Concat("ok","@",certtype,"@",supplyid);
                            }
                            else
                            {
                                rtn = "操作失败！";
                            }
                        }
                        else
                        {
                            rtn = ls;
                        }

                    }
                    catch(Exception ex) {
                        rtn = ex.Message.ToString();
                    }

                    context.Response.Write(rtn);
                    context.Response.End();
                }
                #endregion
                else if (text == "GetInfoDetail")    //获取订单详细信息
                {
                    DataTable dt = _visa.Ajax_GetDetailInfo(detailid);

                    string sb = JsonHelper.DataTableToJson(dt,"DETAIL");
                    context.Response.Write(sb);
                    context.Response.End();
                }
                else if (text == "InsertInfo")    //插入订单
                {
                    string ls_updateid = RequestSession.GetSessionUser().UserId.ToString();
                    string ls_updatename = RequestSession.GetSessionUser().UserName.ToString();
                    string rtn = "";
                                        
                    if (trustid == "s")
                    {
                        trustid = _visa.Info_InsertCustomer(trustname, ls_updateid, ls_updatename);                        
                        rtn = trustid;
                    }

                    //insert
                    bool IsOk = _visa.Ajax_InsertInfo(infoid, ls_updateid, ls_updatename, trustid, info_operation,
                        info_getdate, ls_AddNo);
                    if (IsOk)
                    {
                        rtn = String.Concat("ok", "@", rtn);    //返回新增的trust，用户页面刷新
                    }
                    else
                    {
                        rtn = "操作失败！";
                    }   
                    context.Response.Write(rtn);
                    context.Response.End();
                }
                else if (text == "DeleteInfoDetail")    //删除订单详细信息
                {                    
                    int rtn= _visa.DeleteInfoDetail(infoid);
                    context.Response.Write(rtn);
                    context.Response.End();
                }
            }
        }

        //新建签证类型
        private string InsertCertType(string ls_cityid, string ls_CertTypeID,string ls_CertTypeName,decimal ldc_price,
            string ls_Updateuserid, string ls_Updateusername)
        {            
            //插入签证信息
            ls_CertTypeID = _visa.Info_InsertCertType(ls_CertTypeName, ls_Updateuserid, ls_Updateusername);
                
            //插入国家签证信息
            _visa.Info_InsertCertCity(ls_cityid, ls_CertTypeID, ls_Updateuserid, ls_Updateusername, ldc_price);

            return ls_CertTypeID;
        }

        //新建供应商
        private string InsertSupply(string ls_cityid,string ls_certtypeid, string ls_SupplyID, string ls_SupplyName, decimal ldc_price,
            string ls_Updateuserid, string ls_Updateusername)
        {
            //插入供应商
            ls_SupplyID = _visa.Info_InsertSupply(ls_SupplyName, ls_Updateuserid, ls_Updateusername);
            
            //插入供应商报价
            _visa.Info_SupplyPrice(ls_SupplyID, ls_cityid, ls_certtypeid, ldc_price, ls_Updateuserid, ls_Updateusername);

            return ls_SupplyID;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}